/**
 * @(#)CreateAccountActivity.java        1.0 2/22/12
 * 
 * Copyright (c) 2012 public static droid main
 * Georgia Institute of Technology, Atlanta, GA
 * All rights reserved.
 */

package edu.gatech.oad.publicstaticdroidmain.taskmanager.ui;

import edu.gatech.oad.publicstaticdroidmain.taskmanager.model.Database;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An Android activity that allows a user to create an account.  
 * Contains a form for entering user information.
 * 
 * @version 1.0 Feb 22 2012
 * @author Raheel Merali
 */
public class CreateAccountActivity extends AbstractTaskManagerActivity {

    private OnClickListener buttonListener;

    private TextView nameTextField;
    private TextView emailAddressTextField;
    private TextView usernameTextField;
    private TextView passwordTextField;
    private Button createAccountButton;

    private Database db;

    /**
     * Sets up the layout and captures the form elements into Java objects.
     * 
     * @param savedInstanceState saved data to reload
     */
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.createaccount);
    	
    	nameTextField = (TextView) findViewById(R.id.createAccountNameTextField);
    	emailAddressTextField = (TextView) findViewById(R.id.createAccountEmailTextField);
    	usernameTextField = (TextView) findViewById(R.id.createAccountUsernameTextField);
    	passwordTextField = (TextView) findViewById(R.id.createAccountPasswordTextField);
    	createAccountButton = (Button) findViewById(R.id.createAccountCreateButton);
    	
    	buttonListener = new CreateAccountActivityButtonListener();
    	createAccountButton.setOnClickListener(buttonListener);
    	
    	db = new Database(CreateAccountActivity.this);
    }

    
    /**
     * Called when the "Create" button is pressed.  Handles the actual account
     * creation in the database.
     */
    private class CreateAccountActivityButtonListener implements OnClickListener {
	public void onClick(View view)
	{
	    String name = nameTextField.getText().toString();
	    String email = emailAddressTextField.getText().toString();
	    String username = usernameTextField.getText().toString();
	    String password = passwordTextField.getText().toString();

	    db.open();
	    
	    if (db.validUsername(username)) {
	    	db.createUser(username, password, email, name);	    	
	    	showSuccessToast("Congratulations! You have successfully created an account");
	    	startUIActivity(getString(R.string.activity_login));
	    } else { //tell the user that the account name is already taken
		showErrorToast("This username is already taken.");
	    }
	    
	    db.close();
	} //end onClick method
    } //end CreateAccountActivityButtonListener
    
} //end CreateAccountActivity
